package com.hifar.common;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;



public class SearchParamBean {

	private List<String> orderStringList=new ArrayList<String>();
	
	

	private List<SearchCondition> conditionList = new ArrayList<SearchCondition>();

	private String whereSql;
	private String orderSql;

	public void generateSql() {
		StringBuffer sb = new StringBuffer();
		StringBuffer sb2 = new StringBuffer();
		SearchCondition qc = null;
		Object value=null;
		Collection collect1 = null;
		Iterator iter=null;
		for (int i = 0; i < conditionList.size(); i++) {
			qc = conditionList.get(i);
			sb.append(" and ");
			sb.append(qc.getColumnName());
			sb.append(" " + qc.getOperate() + " ");
			value=qc.getSearchValue();
			
			if(value instanceof Collection) {
				collect1=(Collection) value ;
				iter=collect1.iterator();
				sb2 = new StringBuffer();
				if(collect1.size()>0) {
					 sb2.append("  ( ");
					 while(iter.hasNext()) {
						 sb2.append("'"+iter.next()+"',");
					 }
					 sb2.deleteCharAt(sb2.lastIndexOf(","));
					 sb2.append(" ) ");
					 sb.append(sb2.toString() );
				}else {
					sb.append("  ('-1')  ");
				}
			}else {
				sb.append("'" + qc.getSearchValue() + "'");
			}
			sb.append("  ");
		}
		this.whereSql=sb.toString();
		sb = new StringBuffer();
		if (orderStringList.size() > 0) {
			sb.append(" order by ");
			for (String key1 : orderStringList) {
				sb.append(key1 +",");
			}
			sb.deleteCharAt(sb.lastIndexOf(","));
			this.orderSql = sb.toString();
		}
	}

	

	public List<SearchCondition> getConditionList() {
		return conditionList;
	}

	public void setConditionList(List<SearchCondition> conditionList) {
		this.conditionList = conditionList;
	}

	public String getWhereSql() {
		generateSql();
		return whereSql;
	}

	public void setWhereSql(String whereSql) {
		
		this.whereSql = whereSql;
	}

	

	public List<String> getOrderStringList() {
		return orderStringList;
	}

	public void setOrderStringList(List<String> orderStringList) {
		this.orderStringList = orderStringList;
	}

	public String getOrderSql() {
		return orderSql;
	}

	public void setOrderSql(String orderSql) {
		this.orderSql = orderSql;
	}

	@Override
	public String toString() {
		return "SearchParamBean [orderStringList=" + orderStringList + ", conditionList=" + conditionList.toString()
				+ ", whereSql=" + whereSql + ", orderSql=" + orderSql + "]";
	}

	

}
